package lhy.autoparts.servlet;

import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lhy.AppStart;
import lhy.frontend.FrontendUtils;
import lhy.orm.OrmUtils;
import lhy.orm.Serializer;
import lhy.orm.dao.GenericDaoImpl;
import lhy.utils.ReflectUtils;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;

@WebServlet("/UniCrudServlet")
public class UniCrudServlet extends Utf8Servlet {
    /*
        通过实体类来获取表格全部数据。
        传入：entityClassName com.cxstock.pojo.XXX 实体类完整名称
            condition  where开头的语句
            返回：json
        */
    @Override
    protected void doGet(HttpServletRequest que, HttpServletResponse pon) {
        super.doGet(que, pon);
        String entityClassName = que.getParameter("entityClassName");
        String condition = que.getParameter("condition");

        Class<?> clz = ReflectUtils.loadClassByName(entityClassName);
        List<?> objects;
        if(StringUtils.isEmpty(condition))
            objects = new GenericDaoImpl<>(clz).queryForAll();
        else
            objects = new GenericDaoImpl<>(clz).queryByCondition(condition);
        write(pon,Serializer.doSerialize(objects.toArray()));
    }

    //添加模式，传入表格数据和entityClassName
    @Override
    protected void doPost(HttpServletRequest que, HttpServletResponse pon) {
        super.doPost(que, pon);
        Map<String, Object> paramMapFromAjax = FrontendUtils.getParamMapFromAjax(que);
        String id = (String) paramMapFromAjax.get("id");
        /*自增ID时如果为空就去掉*/
        if(StringUtils.isEmpty(id)){
            paramMapFromAjax.remove("id");
        }
        AppStart.LOGGER.info("表格添加数据，数据"+paramMapFromAjax);

        Class<?> entityClass = ReflectUtils.loadClassByName(String.valueOf(paramMapFromAjax.get("entityClassName")));
        paramMapFromAjax.remove("entityClassName");

        Object object = FrontendUtils.getFormAjaxObject(paramMapFromAjax, entityClass);
        new GenericDaoImpl<>(entityClass).create(object);
    }
    //修改模式 同理
    @Override
    protected void doPut(HttpServletRequest que, HttpServletResponse pon) {
        super.doPut(que, pon);
        Map<String, Object> paramMapFromAjax = FrontendUtils.getParamMapFromAjax(que);
        AppStart.LOGGER.info("表格修改数据，数据"+paramMapFromAjax);

        Class<?> entityClass = ReflectUtils.loadClassByName(String.valueOf(paramMapFromAjax.get("entityClassName")));
        paramMapFromAjax.remove("entityClassName");
        Field identityField = OrmUtils.getIdentityField(entityClass);

        Object object = FrontendUtils.getFormAjaxObject(paramMapFromAjax, entityClass);
        Object id = ReflectUtils.getFieldValue(identityField,object);
        new GenericDaoImpl<>(entityClass).update(object);
    }
    //删除模式，传进来id名字，id值，和entityClassName
    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) {
        super.doDelete(req, resp);
        Map<String, Object> paramMap = FrontendUtils.getParamMapFromAjax(req);
        AppStart.LOGGER.info("传入数据"+paramMap.toString());
        String entityClassName = String.valueOf(paramMap.get("entityClassName"));
        String idFieldName = String.valueOf(paramMap.get("idFieldName"));
        String idValue = String.valueOf(paramMap.get("idValue"));
        Class<?> entityClass = ReflectUtils.loadClassByName(entityClassName);
        new GenericDaoImpl<>(entityClass).delete(idValue);
        /*Map<String, Object> paramMapFromAjax = FrontendUtils.getParamMapFromAjax(req);
        Class<?> entityClass = ReflectUtils.loadClassByName(String.valueOf(paramMapFromAjax.get("ENTITY_CLASS_NAME")));
        paramMapFromAjax.remove("ENTITY_CLASS_NAME");
        Object object = FrontendUtils.getFormAjaxObject(paramMapFromAjax, entityClass);
        try {
            Field identityField = OrmUtils.getIdentityField(entityClass);
            new GenericDaoImpl<>(entityClass).delete(identityField.getName(),identityField.get(object));
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }*/
    }
}
